#!/usr/bin/env bash
# Tags: no-fasttest,no-parallel-replicas
# no-parallel-replicas: the ProfileEvents with the expected values are reported on the replicas the query runs in,
# and the coordinator does not collect all ProfileEvents values.

CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh

# test null
$CLICKHOUSE_CLIENT --query_id="test_03714_1_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_boolean is not null order by f_int_nn
"
$CLICKHOUSE_CLIENT --query_id="test_03714_2_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_boolean is not null order by f_int_nn settings use_paimon_partition_pruning=1
"

# test date range
$CLICKHOUSE_CLIENT --query_id="test_03714_3_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_date_nn >= '2023-01-01' and f_date_nn <= '2023-01-02' order by f_int_nn 
"
$CLICKHOUSE_CLIENT --query_id="test_03714_4_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_date_nn >= '2023-01-01' and f_date_nn <= '2023-01-02' order by f_int_nn settings use_paimon_partition_pruning=1
"

# test timestamp
$CLICKHOUSE_CLIENT --query_id="test_03714_5_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_timestamp_nn >= '2025-01-01 00:00:00.001' and f_timestamp_nn <= '2025-01-03 02:02:02.001' order by f_int_nn 
"
$CLICKHOUSE_CLIENT --query_id="test_03714_6_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_timestamp_nn >= '2025-01-01 00:00:00.001' and f_timestamp_nn <= '2025-01-03 02:02:02.001' order by f_int_nn settings use_paimon_partition_pruning=1
"

# test multiple filters
$CLICKHOUSE_CLIENT --query_id="test_03714_7_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_date_nn >= '2023-01-01' and f_date_nn <= '2023-01-02' and f_boolean is not null and f_string = '中文String1' order by f_int_nn 
"
$CLICKHOUSE_CLIENT --query_id="test_03714_8_$CLICKHOUSE_TEST_UNIQUE_NAME" --enable_time_time64_type=1 --session_timezone="UTC" --query "
select f_boolean,
f_char,
f_varchar,
f_string,
f_binary,
f_varbinary,
f_bytes,
f_decimal,
f_decimal2,
f_decimal3,
f_tinyint,
f_smallint,
f_int,
f_bigint,
f_float,
f_double,
f_date,
f_time,
f_timestamp,
f_timestamp2,
toTimeZone(f_timestamp3, 'Asia/Shanghai'),
f_boolean_nn,
f_char_nn,
f_varchar_nn,
f_string_nn,
f_binary_nn,
f_varbinary_nn,
f_bytes_nn,
f_decimal_nn,
f_decimal2_nn,
f_decimal3_nn,
f_tinyint_nn,
f_smallint_nn,
f_int_nn,
f_bigint_nn,
f_float_nn,
f_double_nn,
f_date_nn,
f_time_nn,
f_timestamp_nn,
f_timestamp2_nn,
toTimeZone(f_timestamp3_nn, 'Asia/Shanghai'),
f_array,
f_map from paimonS3(s3_conn, filename='paimon_all_types') where f_date_nn >= '2023-01-01' and f_date_nn <= '2023-01-02' and f_boolean is not null and f_string = '中文String1' order by f_int_nn settings use_paimon_partition_pruning=1
"


$CLICKHOUSE_CLIENT --query "
    SYSTEM FLUSH LOGS query_log;
"

$CLICKHOUSE_CLIENT --query "
        SELECT sum(ProfileEvents['EngineFileLikeReadFiles']) FROM system.query_log 
        WHERE initial_query_id like '%test_03714%' and initial_query_id like '%$CLICKHOUSE_TEST_UNIQUE_NAME%' AND 
        current_database = currentDatabase() and type='QueryFinish' group by initial_query_id order by initial_query_id;"


